//
//  DayNightManager.m
//  one·一个  tyw
//
//  Created by tyw on 15/8/27.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "DayNightManager.h"


@implementation DayNightManager {
    NSString* currentDayNightType;      //当前模式
}

+ (instancetype)sharedManager {
    static id instance;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        instance = [[[self class] alloc] init];
    });
    
    return instance;
}

- (instancetype)init {
    if (self = [super init]) {
        //从本地获得上次存储着的日间夜间模式
        NSString* saveType = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultsDayNightType];
        if (saveType.length > 0) {     //如果存在，就按上次的模式来
            currentDayNightType = saveType;
        } else {        //如果不存在，就默认设置为日间模式
//            NSLog(@"%@", kDayNightCurrentTypeIsDay);
            currentDayNightType = kDayNightCurrentTypeIsDay;
        }
        [self changeCurrenDayNightType:currentDayNightType];
    }
    return self;
}

- (void)changeCurrenDayNightType:(NSString *)type {
    if (currentDayNightType != type) {
        currentDayNightType = type;
        
        //01 把数据存储到本地
        [[NSUserDefaults standardUserDefaults] setObject:currentDayNightType forKey:kUserDefaultsDayNightType];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        //02 发送通知，告诉大家模式改变了
        [[NSNotificationCenter defaultCenter] postNotificationName:kDayNightTypeChangedNotification object:currentDayNightType];
    }
}

- (NSString *)getCurrentDayNightType {
    return currentDayNightType;
}


- (UIImage *)getThemeImage:(NSString *)imageName {
    if (imageName.length == 0) {
        NSLog(@"图片名为空,设置无效");
        return nil;
    }
    //查看当前状态，是日间还是夜间.根据日间夜间，拼接图片名称
    if ([currentDayNightType isEqualToString:kDayNightCurrentTypeIsDay]) {      //日间
        UIImage* image = [UIImage imageNamed:imageName];
        return image;
    } else {                                                                //夜间
        NSString* str = [imageName stringByAppendingString:@"_nt"];
        UIImage* image = [UIImage imageNamed:str];
        return image;
    }
}

@end
